package com.skytech.component.crawler.parse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.scratch.base.*;
import com.skytech.component.crawler.util.ElementAbstractUrl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.chrome.ChromeDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class PartTask {

    @Autowired
    private CrawlerUrlDAO crawlerUrlDAO;
    @Autowired
    private ParseHtmlService parseHtmlService;
    @Autowired
    private PageContentDAO pageContentDAO;
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    @Value("${elasticsearch.server}")
    private String server;

    @Value("${chrome.driver.path}")
    public String driverPath;

    private static final Logger log = LoggerFactory.getLogger(ParserLoader.class);
    @Async
    public void partTask(List<CrawlerUrl> crawlerUrls, Map<String,TargetRules> rulesMap, Integer index){
        /*System.setProperty("webdriver.chrome.driver", driverPath);
        ChromeDriver driver =new ChromeDriver();*/
        log.info("task--{}--任务开始",index);
        PageContent pageContent = null;
        for (CrawlerUrl url : crawlerUrls) {

            if(!rulesMap.containsKey(url.getRuleId().trim())){
                continue;
            }
            try{
                Document document = null;
                /*driver.get(url.getContentUrl());*/
                try {
                    document = Jsoup.connect(url.getContentUrl())
                            .timeout(10000)
                            .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                            .header("Accept-Encoding", "gzip, deflate, sdch")
                            .header("Accept-Language", "zh-CN,zh;q=0.8")
                            .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36").get();
                    /*driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);*/
                } catch (Exception e) {
                    log.error("规则 : {}--url : {}--错误原因 : {}",url.getRuleId(),url.getContentUrl(),e.getMessage());
                    continue;
                }
                /*String html = driver.findElementByTagName("html").getAttribute("innerHTML");*/
                /*Document document = Jsoup.parse(html, driver.getCurrentUrl());*/
                Elements allElements = document.getAllElements();
                ElementAbstractUrl.transform(allElements);
                TargetRules targetRules = rulesMap.get(url.getRuleId());
                String title = parseHtmlService.parseTitle(allElements,rulesMap.get(url.getRuleId()).getTitleSelect());
                String content = parseHtmlService.parseContent(allElements,rulesMap.get(url.getRuleId()).getContentSelect());

                PageView pageView = new PageView();
                pageContent = new PageContent();
                pageContent.setId(UUID.randomUUID().toString());
                pageContent.setCatalog(targetRules.getCatalog());
                pageContent.setNewsType(targetRules.getNewsType());
                pageContent.setContent(content);
                pageContent.setTitle(title);
                pageContent.setPublish("no");
                pageContent.setStorage("no");
                pageContent.setAbandon("no");
                pageContent.setTimestamp(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                pageContent.setSourceUrl(url.getContentUrl());
                pageContent.setRuleId(url.getRuleId());
                pageContent.setUrlId(url.getId());
                pageContent.setTarget("no");

                pageView.setId(UUID.randomUUID().toString());
                pageView.setCatalog(targetRules.getCatalog());
                pageView.setNewsType(targetRules.getNewsType());
                pageView.setContent(content);
                pageView.setTitle(title);
                pageView.setPublish("no");
                pageView.setStorage("no");
                pageView.setAbandon("no");
                pageView.setTimestamp(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                pageView.setSourceUrl(url.getContentUrl());
                pageView.setRuleId(url.getRuleId());
                pageView.setUrlId(url.getId());
                pageView.setTarget("no");
                List<String> keywordsList;

                if("project".equals(targetRules.getNewsType())){
                    keywordsList = CrawlerKeywords.projectKeyWords;
                }else{
                    keywordsList = CrawlerKeywords.policyKeyWords;
                }
                for (String projectKeyWord : keywordsList) {
                    if(!StringUtils.isEmpty(pageView.getTitle())&&(pageView.getTitle().indexOf(projectKeyWord)!=-1)){
                        pageView.setTarget("yes");
                        pageContent.setTarget("yes");
                        break;
                    }
                    if(!StringUtils.isEmpty(pageView.getContent())&&(pageView.getContent().indexOf(projectKeyWord)!=-1)){
                        pageView.setTarget("yes");
                        pageContent.setTarget("yes");
                        break;
                    }
                }
                url.setParse("yes");
                try{
                    saveData(pageContent,url,pageView,index);
                }catch (Exception e){
                    log.error("task--{}--{}--解析失败--cause{}",index,url.getContentUrl(),e.getMessage());
                }

            }catch (Exception e){
                log.error("task--{}--页面链接错误--{}--info--{}",index,url.getContentUrl(),e.getMessage());
            }

            // todo more field
        }
        log.info("task--{}--任务结束",index);
       /* driver.close();*/
    }

    @Transactional
    protected void saveData(PageContent pageContent, CrawlerUrl url,PageView pageView,Integer index){
       /* pageContentDAO.save(pageContent);*/
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(pageView));
        jsonObject.remove("id");
        restTemplateBuilder.build().put(server+"crawler_news/news/"+pageView.getId(),jsonObject);
        crawlerUrlDAO.save(url);
        log.info("task--{}--解析完成--{}",index,url.getContentUrl());
    }
}
